﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.Text;

namespace Ccit.SysPlat.Organ
{
    /// <summary>
    /// Description: 系统平台组织机构 -> 级别管理维护页面
    /// Author: tim 2010-02-25
    /// </summary>
    public partial class sys_class_edit : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _hidOrganId.Value = (null != Request.QueryString["organId"]) ? Request.QueryString["organId"].ToString() : "1";
                _hidClsId.Value = (null != Request.QueryString["clsId"]) ? Request.QueryString["clsId"].ToString() : string.Empty;
                initializePage();
            }
        }

        private void initializePage()
        {
            ClsBLL clsBLL = new ClsBLL();
            string clsId = _hidClsId.Value;

            initAddNew();
            MsClass msCls = clsBLL.GetClsInfo(clsId);
            initEdit(msCls);

            msCls = null;
            clsBLL.Release();
            clsBLL = null;
        }

        private void initAddNew()
        {
            txtClassName.Text = string.Empty;
            txtClassDesc.Text = string.Empty;
            _hidSelUser.Value = string.Empty;
            txtSelUser.Text = string.Empty;
            txtClassOrder.Text = string.Empty;
        }

        private void initEdit(MsClass msCls)
        {
            if (null == msCls) return;

            txtClassName.Text = msCls.clsName;
            txtClassDesc.Text = msCls.clsDesc;
            _hidSelUser.Value = msCls.includeUsers.key;
            txtSelUser.Text = msCls.includeUsers.val.Trim().Replace(" ","\r\n");
            txtClassOrder.Text = msCls.orderNo;
        }


        #region ajax methods

        /// <summary>
        /// 校验是否存在同名级别
        /// </summary>
        /// <param name="clsId"></param>
        /// <param name="clsName"></param>
        /// <param name="organId"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string CheckClassNameExist(string clsId, string clsName, string organId)
        {
            ClsBLL clsBLL = new ClsBLL();
            List<string> lstResult = clsBLL.IsExistClsName(clsName, clsId, organId);
            var newObj = new {
                OPERTYPE = "CLSNAMECHECK",
                RSTSIGN = lstResult[0],
                RSTMSG = lstResult[1]
            };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);

            newObj = null;
            lstResult = null;
            clsBLL.Release();
            clsBLL = null;
            return serExpr;
        }

       /// <summary>
       /// 保存级别信息
       /// </summary>
       /// <param name="jsonExpr"></param>
       /// <param name="ctrlType"></param>
       /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string SaveClsData(string jsonExpr,string ctrlType)
        {
            MsClass msCls = JavaScriptConvert.DeserializeObject(jsonExpr, typeof(MsClass)) as MsClass;
            ClsBLL clsBLL = new ClsBLL();
            List<string> lstResult = clsBLL.Save(msCls);
            var newObj = new {
                OPERTYPE = "CLSSAVE",
                RSTSIGN = lstResult[0],
                RSTMSG = lstResult[1],
                CTRLTYPE = ctrlType
            };

            string serExpr = JavaScriptConvert.SerializeObject(newObj);

            newObj = null;
            lstResult = null;
            clsBLL.Release();
            clsBLL = null;
            return serExpr;
        }

        /// <summary>
        /// 删除级别
        /// </summary>
        /// <param name="sClassXML"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DeleteCls(string clsId)
        {
            ClsBLL clsBLL = new ClsBLL();
            List<string> lstResult = clsBLL.Delete(clsId);
            var newObj = new {
                OPERTYPE = "CLSDELETE",
                RSTSIGN = lstResult[0],
                RSTMSG = lstResult[1]
            };

            string serExpr = JavaScriptConvert.SerializeObject(newObj);

            newObj = null;
            lstResult = null;
            clsBLL.Release();
            clsBLL = null;
            return serExpr;
        }

        #endregion
    }
}
